package cn.wolfcode.crm.web.Controller;

import cn.wolfcode.crm.Service.ICustomerService;
import cn.wolfcode.crm.Service.ICustomerTransferService;
import cn.wolfcode.crm.Service.IEmployeeService;
import cn.wolfcode.crm.Service.ISystemDictionaryItemService;
import cn.wolfcode.crm.domain.Customer;
import cn.wolfcode.crm.domain.CustomerTransfer;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.SystemDictionaryItem;
import cn.wolfcode.crm.query.CustomerQueryObject;
import cn.wolfcode.crm.query.CustomerTransferQueryObject;
import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

//shiro需要在用户访问响应资源的时候对其权限做检查
//因为需要在访问每个资源的时候都做检查,所以需要使用通用的代码
//shiro使用动态代理的方式来实现
//动态代理的使用场景:对目标方法做功能增强的时候(原本没有的功能.增强的时候赋予)
@Controller
@RequestMapping("/customerTransfer")
public class CustomerTransferController {
    @Autowired
    private ICustomerService customerService;
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;
    @Autowired
    private ICustomerTransferService customerTransferService;

    //shiro中的value只是权限表达式,但我们将用作其他用途
    //RequiresPermissions:当前方法是否用户value中指定的的所有权限
    //如果有,运行访问,否则不能访问
    //logical:检查权限的时候,是都需要(AND),还是有一个就行(OR)
    @RequiresPermissions(value = {"移交列表","customerTransfer:list"},logical = Logical.OR)
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") CustomerTransferQueryObject qo) {
        //如果不是超管并且不是销售经理,将当前用户的id作为sellerId来做查询
        Employee currentEmp = (Employee) SecurityUtils.getSubject().getPrincipal();

        if (!(currentEmp.isAdmin() || SecurityUtils.getSubject().hasRole("Market_Manager"))){
            qo.setSellerId(currentEmp.getId());
        }


        PageInfo<CustomerTransfer> pageResult = customerTransferService.queryForList(qo);
        model.addAttribute("pageResult", pageResult);
        return "customerTransfer/list";//跳转到list.jsp页面/WEB-INF/view/customerTransfer/list.flt
    }



    @RequiresPermissions(value = {"移交保存或修改","customerTransfer:saveOrUpdate"},logical = Logical.OR)
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(CustomerTransfer customerTransfer) {
        JsonResult result = new JsonResult();
        try {
            System.out.println(3);
            if (customerTransfer.getId() == null) {
                customerTransferService.insert(customerTransfer);
            } else {
                customerTransferService.updateByPrimaryKey(customerTransfer);
            }
        } catch (Exception e) {
                e.printStackTrace();
                result.mark("保存失败");
            }

        return result;
    }



}
